<template>
	<view class="container">
		<view class="commentbox" v-for="(item,index) in ordergoods">
			<view class="productbox">
				<view class="productimg">
					<image :src="item.picUrl" mode=""></image>
				</view>
				<view class="producttext">
					<view class="text-top">
						{{item.goodsName}}
					</view>
					<view class="text-buttom">
						{{item.specifications}}×{{item.number}}
					</view>
				</view>
			</view>
			<view class="rate">
			<text>描述符合</text>	<u-rate :count="count" v-model="ratevalue" ></u-rate>
			</view>
			<view class="textarea">
				<u--textarea v-model="textvalue" placeholder="请输入内容" count />
			</view>
			<view class="upload">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" @oversize="oversize" name="1"
					:previewFullImage="true" multiple :maxCount="3" :max-size="5*1024*1024"/>
			</view>
			<view class="button">
				<u-button text="确定" shape="circle" type="success" @click="jump"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				filebase:[], //上传图片转为base84
				textvalue:'',
				productid:0, //商品id
				orderid:0,  
				ratevalue:0,
				count:5,
				ordergoods:[]
			}
		},
		onLoad(options) {
			this.productid=options.product
			this.getorder(options.product)
			this.orderid=options.order
		},
		methods: {
			//删除
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.filebase.splice(event.index,1)
			},
			oversize() {
				uni.showToast({
					title: "图片最大不能超过5M",
					icon: 'none'
				})
				
			},
			//获取上传图片并显示
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].thumb)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}

			},
			//把上传图片转为base64存入数组里
			uploadFilePromise(url) {
				let that=this
				const fs=uni.getFileSystemManager()
				fs.readFile({
					filePath:url,
					encoding:'base64',
					success:res=>{
					   this.filebase.push('data:image/jpeg;base64,'+res.data)
					}
				})
			},
			getorder(id){
				this.$get(this.API.orderdetail,{
					orderId:id
				}).then(res=>{
					this.ordergoods=res.data.data.orderGoods
				})
			},
			//提交评价
			jump(){
				this.$post(this.API.commentpost,{
					orderGoodsId:this.orderid,
					content:this.textvalue,
					star:this.ratevalue,
					hasPicture:Boolean(this.filebase.length),
					filePath:this.filebase
				}).then(res=>{
					console.log(res.data)
					if(res.data.errno==0){
						uni.showToast({
							title:'评价成功',
							icon:'none'
						})
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/orderlist/orderlist'
							})
						},2000)
					}else{
						uni.showToast({
							title:res.data.errmsg,
							icon:'error'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
 .container{
	 .commentbox{
		 width: 90vw;
		 margin: 20rpx auto;
		 border-radius: 20rpx;
		 background-color: #E5E5E5;
		 padding: 20rpx;
	.productbox{
		display: flex;
		height: 10vw;
		margin-bottom: 30rpx;
		.productimg{
			width: 15vw;
			image{
				height: 5vh;
				width: 10vw;
			}
		}
	}
	.rate{
		margin: 20rpx 0;
		display: flex;
		
	}
	.textarea{
		width: 90vw;
		margin: 20rpx 0;
	}
	.button{
		width: 90vw;
		margin: 20rpx 0;
	}
	 }
 }
</style>
